package com.qf.ts.platform.front.service.impl;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.qf.ts.platform.commons.bean.User;
import com.qf.ts.platform.commons.constant.SystemParams;
import com.qf.ts.platform.front.feign.CacheService;
import com.qf.ts.platform.front.mapper.UserMapper;
import com.qf.ts.platform.front.service.UserService;
import com.qf.ts.platform.front.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CacheService cacheService;

    @Transactional(rollbackFor = Exception.class)
    @LcnTransaction
    @Override
    public Integer addUser(User user) {

        String uuid = UUID.randomUUID().toString().substring(0,15) + System.currentTimeMillis();

//        System.out.println(uuid);

        user.setUuid(uuid);

        String newPwd = MD5Util.md5(user.getPassword());

        user.setPassword(newPwd);

        int result = userMapper.addUser(user);

//        System.out.println(result);
//        System.out.println(user);

        //结果成功时,添加session缓存
        if(result > 0){
            Integer id = userMapper.getIdByUuid(uuid);

            System.out.println(id);

            user.setId(id);

            try {
//                int i = 1 / 0;
                cacheService.hMSet(SystemParams.USER_REDIS_PRE + user.getId(),user);
            } catch (Exception e) {
                e.printStackTrace();
            }


            return SystemParams.SQL_CODE_SUCCESS;

        }

        return SystemParams.SQL_CODE_FAIL;

    }

    @Override
    public User findUser(User user) {

        User newuser = userMapper.findUser(user);

        if(newuser != null){

            return newuser;
        }

        return null;


    }

    @Override
    public User getUserByUser(User user) {
        return null;
    }

    @Override
    public List<String> selectRolesByUid(Integer id) {
        return null;
    }

    @Override
    public List<String> selectPermissionsByUid(Integer id) {
        return null;
    }


}
